package com.alstudio.ui.module.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alstudio.R;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.notification.SystemNotificationEnvConfig;

public class SystemSettingActivity extends TitleBarActivity {

	private final String TAG = "SystemSettingActivity";
	private boolean soundState;
	private boolean vbrationState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, TAG);
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		// 设置本界面的UI
		setTitleBarContentView(R.layout.system_setting_layout);
		setLeftButtonBackground(R.drawable.title_bar_back_button_selector);
		hideRightButton(true);
		setCenterTextViewMessage(R.string.TxtSystemSettingTitle);
		findViewById(R.id.sound_switcher_item).setOnClickListener(this);
		findViewById(R.id.vbration_switcher_item).setOnClickListener(this);

		soundState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getSoundState();
		vbrationState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getVbrationState();

		// 更新系统设置的UI
		updateSwitcherState();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sound_switcher_item:
			updateSoundState();
			break;
		case R.id.vbration_switcher_item:
			updateVbrationState();
			break;
		}
	}

	/**
	 * 刷新声音开关状态
	 */
	private void updateSoundState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setSoundState(!soundState);
		soundState = !soundState;
		updateSwitcherState();
	}

	/**
	 * 刷新振动开关状态
	 */
	private void updateVbrationState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setVbrationState(!vbrationState);
		vbrationState = !vbrationState;
		updateSwitcherState();
	}

	/**
	 * 更新系统设置相关属性的UI
	 */
	private void updateSwitcherState() {
		// 更新声音开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getSoundState()) {
			findViewById(R.id.sound_state).setBackgroundResource(
					R.drawable.multi_choice_on);
		} else {
			findViewById(R.id.sound_state).setBackgroundResource(
					R.drawable.multi_choice_off);
		}

		// 更新振动开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getVbrationState()) {
			findViewById(R.id.vbration_state).setBackgroundResource(
					R.drawable.multi_choice_on);
		} else {
			findViewById(R.id.vbration_state).setBackgroundResource(
					R.drawable.multi_choice_off);
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

}
